import { defineStore } from 'pinia'
import type {
  GoodsList,
  selectGoodsT,
  approveType,
  status
} from '@/service/goods/type'
import {
  selectGoodsRequest,
  insertApproveRequest,
  selectApproveRequest,
  selectPurchaseRequest,
  deleteTableRequest
} from '@/service/goods'

export const usegoods = defineStore('goods', {
  state: () => {
    return {
      GoodsList: {} as GoodsList,
      approveList: {} as GoodsList,
      purchaseList: {} as GoodsList,
      mockList: [] as any,
      status: 'success' as status
    }
  },
  getters: {
    records: (state) => (module: string) => {
      return (state as any)[`${module}List`].records
    },
    listCount: (state) => (module: string) => {
      return (state as any)[`${module}List`].total
    }
  },
  actions: {
    //找出未被删除的元素
    async getList(arr: any) {},
    //获取物资列表
    async selectGoods(payload: selectGoodsT) {
      const resoult = await selectGoodsRequest(payload)
      this.GoodsList = resoult.data
      this.mockList = resoult.data
    },
    //提交物资申请表
    async approveGoods(payload: approveType) {
      const resoult = await insertApproveRequest(payload)
      if (resoult.code === 0) {
        this.status = 'success'
      }
    },
    //获取物资申请表列表
    async selectApprove(payload: selectGoodsT) {
      const resoult = await selectApproveRequest(payload)
      console.log(resoult.data)

      this.approveList = resoult.data
    },
    //获取物资采购表列表
    async selectPurchase(payload: selectGoodsT) {
      const resoult = await selectPurchaseRequest(payload)
      this.purchaseList = resoult.data
    },

    //删除表
    async deleteTable(module: string, payload: number[]) {
      const resoult = await deleteTableRequest(module, payload)
      this.status = resoult.data
    }
  }
})
